package com.angus.auth.controller;

import com.angus.api.front.model.FrontLoginUser;
import com.angus.auth.form.PhoneLoginGroup;
import com.angus.auth.form.SendCodeGroup;
import com.angus.auth.form.front.FrontLoginBody;
import com.angus.auth.form.front.PasswordRestBody;
import com.angus.auth.form.front.DeviceLoginBody;
import com.angus.auth.form.front.RegisterBody;
import com.angus.auth.service.front.FrontLoginService;
import com.angus.common.core.domain.R;
import com.angus.common.core.utils.JwtUtils;
import com.angus.common.core.utils.StringUtils;
import com.angus.common.core.web.controller.BaseController;
import com.angus.common.security.auth.AuthUtil;
import com.angus.common.security.service.TokenService;
import com.angus.common.security.utils.SecurityUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author liyanan
 */
@RestController
@RequestMapping("front")
public class FrontTokenController extends BaseController {

    @Resource
    private FrontLoginService frontLoginService;

    @Resource
    private TokenService tokenService;

    @PostMapping("/login")
    public R<?> login(@RequestBody FrontLoginBody form) {
        // 用户登录
        FrontLoginUser userInfo = frontLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createFrontToken(userInfo));
    }

    @PostMapping("/sms-login")
    @PermitAll
    public R smsLogin(@Validated(PhoneLoginGroup.class) @RequestBody DeviceLoginBody deviceLoginBody, BindingResult bindingResult){

        if (Objects.nonNull(toBindResult(bindingResult))) {
            return R.fail(toBindResult(bindingResult));
        }
        FrontLoginUser userInfo = frontLoginService.sendCodeLogin(deviceLoginBody);
        // 获取登录token
        return R.ok(tokenService.createFrontToken(userInfo));
    }

    @PostMapping("send-code")
    @PermitAll
    public R sendCode(@Validated(SendCodeGroup.class) @RequestBody DeviceLoginBody deviceLoginBody, BindingResult bindingResult){
        if (Objects.nonNull(toBindResult(bindingResult))) {
            return R.fail(toBindResult(bindingResult));
        }
        return R.ok(frontLoginService.sendSendCode(deviceLoginBody));
    }

    @PostMapping("check-code")
    @PermitAll
    public R checkCode(@Validated(SendCodeGroup.class) @RequestBody DeviceLoginBody deviceLoginBody, BindingResult bindingResult){
        if (Objects.nonNull(toBindResult(bindingResult))) {
            return R.fail(toBindResult(bindingResult));
        }
        return R.ok(frontLoginService.checkSendCode(deviceLoginBody));
    }

    @PostMapping("password/reset")
    @PermitAll
    public R<?> resetPassword(@Validated @RequestBody PasswordRestBody passwordRestBody,BindingResult bindingResult) {
        if (Objects.nonNull(toBindResult(bindingResult))) {
            return R.fail(toBindResult(bindingResult));
        }
        frontLoginService.resetPassword(passwordRestBody);
        return R.ok();
    }

    @DeleteMapping("/logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            frontLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("/register")
    public R<?> register(@RequestBody RegisterBody registerBody) {
        frontLoginService.register(registerBody);
        return R.ok();
    }
    @GetMapping("/getUserInfoCount")
    public R<?> getUserInfoCount(){
        return R.ok(frontLoginService.getUserInfoCount());
    }

}
